package com.ukao.cashregister.ui.splash;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseFragment;
import com.ukao.cashregister.bean.ActrivateBean;
import com.ukao.cashregister.bean.SearchHistorysBean;
import com.ukao.cashregister.db.SearchHistorysDao;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.L;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * 条件与调控
 */
public class ClauseFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.back_btn)
    TextView backBtn;

    @BindView(R.id.agree_btn)
    Button agreeBtn;

    Unbinder unbinder;
    private String mParam1;
    private String mParam2;
   private boolean  isActivateCode;
    private String strSerialNum="";
    private SearchHistorysDao dao;
    private ArrayList<SearchHistorysBean> historywordsList;
    private String actCode="";

    public static ClauseFragment newInstance(String param1, String param2) {
        ClauseFragment fragment = new ClauseFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void initView() {
        getSerialNumber();
        isActivateCode=false;
    }

    private void getSerialNumber() {
        dao= new SearchHistorysDao(_mActivity);
        historywordsList= dao.findAll();
        for (SearchHistorysBean searchHistorysBean : historywordsList) {
            if (!searchHistorysBean.historyword.equals("first")){


                try {

                    JSONObject mJSONObject=new JSONObject(searchHistorysBean.historyword);
                    strSerialNum=mJSONObject.getString("serialNum");
                    actCode=mJSONObject.getString("actCode");

                } catch (JSONException e) {


                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_clause, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        isActivate();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.back_btn, R.id.agree_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();


                break;
            case R.id.agree_btn:
        if (isActivateCode){
            start(StartUseFragment.newInstance("",""));
        }else{
            start(BindingCardFragment.newInstance(strSerialNum, ""));
        }

                break;
        }
    }


    /**
     *是否激活序列号   90665439/97018276
     */
    public void isActivate (   ) {
        L.i("serialNum="+strSerialNum);
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("serialNum",strSerialNum);
        addSubscription(apiStores().isActivate(mHashMap),
                new ApiCallback<ActrivateBean>() {
                    @Override
                    public void onSuccess(ActrivateBean model) {
                        if (model.getHttpCode()==200){
                           int  state=model.getData().getState();
                            if (state!=0){/*不为0时可激活*/
                                isActivateCode=true;
                            }
                        }





                    }



                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }

                });
    }
}
